home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / g__~1 / gplibs20.zoo / stdiostr.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-13  |  3.0 KB  |  113 lines

  1. //    This is part of the iostream library, providing -*- C++ -*- input/output.
  2. //    Copyright (C) 1992 Per Bothner.
  3. //
  4. //    This library is free software; you can redistribute it and/or
  5. //    modify it under the terms of the GNU Library General Public
  6. //    License as published by the Free Software Foundation; either
  7. //    version 2 of the License, or (at your option) any later version.
  8. //
  9. //    This library is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. //    Library General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU Library General Public
  15. //    License along with this library; if not, write to the Free
  16. //    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #ifdef __GNUG__
  19. #pragma implementation
  20. #endif
  21.  
  22. #include <stdiostr.h>
  23.  
  24. // A stdiobuf is "tied" to a FILE object (as used by the stdio package).
  25. // Thus a stdiobuf is always synchronized with the corresponding FILE,
  26. // though at the cost of some overhead.  (If you use the implementation
  27. // of stdio supplied with this library, you don't need stdiobufs.)
  28. // This implementation inherits from filebuf, but implement the virtual
  29. // functions sys_read/..., using the stdio functions fread/... instead
  30. // of the low-level read/... system calls.  This has the advantage that
  31. // we get all of the nice filebuf semantics automatically, though
  32. // with some overhead.
  33.  
  34.  
  35. #ifndef SEEK_SET
  36. #define SEEK_SET 0
  37. #endif
  38. #ifndef SEEK_CUR
  39. #define SEEK_CUR 1
  40. #endif
  41. #ifndef SEEK_END
  42. #define SEEK_END 2
  43. #endif
  44.  
  45. stdiobuf::stdiobuf(FILE *f) : filebuf(fileno(f))
  46. {
  47.     _file = f;
  48.     // Turn off buffer in stdiobuf.  Instead, rely on buffering in (FILE).
  49.     // Thus the stdiobuf will be synchronized with the FILE.
  50.     setbuf(NULL, 0);
  51. }
  52.  
  53. _G_ssize_t stdiobuf::sys_read(char* buf, _G_size_t size)
  54. {
  55.     return fread(buf, 1, size, _file);
  56. }
  57.  
  58. _G_ssize_t stdiobuf::sys_write(const void *buf, _G_size_t n)
  59. {
  60.     _G_ssize_t count = fwrite(buf, 1, n, _file);
  61.     if (_fb._offset >= 0)
  62.     _fb._offset += n;
  63.     return count;
  64. }
  65.  
  66. _G_fpos_t stdiobuf::sys_seek(_G_fpos_t offset, _seek_dir dir)
  67. {
  68.     // Normally, equivalent to: fdir=dir
  69.     int fdir =
  70.     (dir == ios::beg) ? SEEK_SET :
  71.         (dir == ios::cur) ? SEEK_CUR :
  72.         (dir == ios::end) ? SEEK_END :
  73.         dir;
  74.     return fseek(_file, offset, fdir);
  75. }
  76.  
  77. int stdiobuf::sys_close()
  78. {
  79.     int status = fclose(_file);
  80.     _file = NULL;
  81.     return status;
  82. }
  83.  
  84. int stdiobuf::sync()
  85. {
  86.     if (filebuf::sync() == EOF)
  87.     return EOF;
  88.     if (!(xflags() & _S_NO_WRITES))
  89.     if (fflush(_file))
  90.         return EOF;
  91. #if 0
  92.     // This loses when writing to a pipe.
  93.     if (fseek(_file, 0, SEEK_CUR) == EOF)
  94.     return EOF;
  95. #endif
  96.     return 0;
  97. }
  98.  
  99. int stdiobuf::overflow(int c /* = EOF*/)
  100. {
  101.     if (filebuf::overflow(c) == EOF)
  102.     return EOF;
  103.     if (c != EOF)
  104.     return c;
  105.     return fflush(_file);
  106. }
  107.  
  108. _G_size_t stdiobuf::xsputn(const char* s, _G_size_t n)
  109. {
  110.     // The filebuf implementation of sputn loses.
  111.     return streambuf::xsputn(s, n);
  112. }
  113.